package com.ishitong.platform.utils.spring;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.ishitong.platform.ODApplicationConfig;
import com.ishitong.platform.ODApplicationConfigLoad;


/**
 * 平台初始化类
 * 
 * @author MarkDong
 * @version 2.0
 * @since 1.0
 */
class ODContextLoader extends ContextLoader {
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Instantiate the root WebApplicationContext for this loader, either the
	 * default context class or a custom context class if specified.
	 * <p>
	 * This implementation expects custom contexts to implement
	 * ConfigurableWebApplicationContext. Can be overridden in subclasses.
	 * 
	 * @param servletContext
	 *            current servlet context
	 * @param parent
	 *            the parent ApplicationContext to use, or <code>null</code>
	 *            if none
	 * @return the root WebApplicationContext
	 * @throws org.springframework.beans.BeansException
	 *             if the context couldn't be initialized
	 * @see org.springframework.web.context.ConfigurableWebApplicationContext
	 */
	protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent) throws BeansException {
		try {
			logger.info("初始化平台......");
			Class contextClass = determineContextClass(servletContext);
			if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
				throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext");
			}
			String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
			// 2 初始化WebApplicationContext
			ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
			wac.setParent(parent);
			wac.setServletContext(servletContext);
			List<String> springConfig = new ArrayList<String>();
			Map<String, ODApplicationConfig> moduleConfig = ODApplicationConfigLoad.getApplicationConfig();
			Iterator ite = moduleConfig.values().iterator();
			while (ite.hasNext()) {
				ODApplicationConfig module = (ODApplicationConfig) ite.next();
				List<String> config = module.getSpringConfig();
				for (int i = 0; i < config.size(); i++) {
					springConfig.add(config.get(i));
				}
			}
			String[] springfile = new String[springConfig.size() + 1];
			springfile[0] = contextConfigLocation;
			for (int i = 0; i < springConfig.size(); i++) {
				springfile[i + 1] = springConfig.get(i);
			}
			wac.setConfigLocations(springfile);
			wac.refresh();
			return wac;
		} catch (BeansException e) {
			logger.error("平台初始化失败", e);
			throw e;
		} catch (IllegalStateException e) {
			logger.error("平台初始化失败", e);
			throw e;
		}
	}
}
